import cv2
import time
from PIL import Image
step = 2
camera_save = True
out_dir = 'camera_data_dacai_2'
start_time = int(time.time())
last_time = start_time
cap = cv2.VideoCapture(0)
  #CV_WINDOW_NORMAL就是0,该句放在imshow之前即可。
cap.set(3, 1280)
cap.set(4, 1440)
# cv2.namedWindow("camera",cv2.WINDOW_NORMAL)
while True:
    ret,frame = cap.read()
    # frame = cv2.rotate(frame, cv2.ROTATE_90_OOUNTERCLOCKWISE)
    # frame=cv2.line(frame,(125,480),(340,225),(0,0,255),1)
    # frame=cv2.line(frame,(140,480),(340,225),(0,0,255),1)

    # frame=cv2.line(frame,(545,480),(340,225),(0,0,255),1)
    # frame=cv2.line(frame,(535,480),(340,225),(0,0,255),1)

    # frame=cv2.line(frame,(150,480),(340,225),(0,255,0),1)
    # frame=cv2.line(frame,(525,480),(340,225),(0,255,0),1)

    # frame=cv2.line(frame,(165,480),(340,225),(255,0,0),1)
    # frame=cv2.line(frame,(507,480),(340,225),(255,0,0),1)
    cv2.namedWindow('camera',cv2.WINDOW_NORMAL)
    cv2.imshow('camera',frame)
    # Image.fromarray(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)).show()
    current_time = int(time.time())
    if camera_save:
        if (current_time-start_time)%step==0 and current_time!=last_time:
            last_time = current_time
            date = time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime())
            print(out_dir+'/'+date+".jpg")
            cv2.imwrite(out_dir+'/'+date+".jpg",frame)
    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()